package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import model.Kunde;
import model.Zimmer;
import view.CreateCustomerView;
import view.CreateRoomView;

public class GUICreateCustomerViewController extends GUIAbstractController
		implements ActionListener {
	private CreateCustomerView customerView;
	private Kunde customer;

	public GUICreateCustomerViewController() {
		super();

		this.customerView = new CreateCustomerView();
		this.customerView.addActionsListeners(this);
	}

	public GUICreateCustomerViewController(Kunde customerToEdit) {
		this();
		customer = customerToEdit;
		updateGuiFieldsFromRoom();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.customerView.getBtnCancel()) {
			System.out.println("Cancel Button");
			this.customerView.setVisible(false);

		} else if (e.getSource() == this.customerView.getBtnSave()) {
			System.out.println("Save Button");
			updateCustomerFromGuiFields();
			saveOrUpdateCustomer();
			this.customerView.setVisible(false);
		}
	}

	private void saveOrUpdateCustomer() {
		if (customer.getId() > 0) {
			// update
			super.dbController.updateEntity(customer);
		} else {
			// insert new
			super.dbController.saveEntity(customer);
		}
	}

	public CreateCustomerView getCustomerView() {
		return customerView;
	}

	private void updateCustomerFromGuiFields() {

		if (customer == null) {
			customer = new Kunde();
		}
		customer.setVorname(customerView.getTxtFirstName().getText());
		customer.setNachname(customerView.getTxtSecondName().getText());
		customer.setRechnungsadresse(customerView.getTxtAdressOfBill()
				.getText());
		customer.setFirmenname(customerView.getTxtCompanyname().getText());
		customer.setNotiz(customerView.getTxtNotice().getText());
		customer.setRabat(TextFieldParsingHelper
				.parseDoubleFromTextField(customerView.getTxtDiscount()));
		customer.setTelefon(customerView.getTxtTelephoneNumber().getText());
		customer.setEmail(customerView.getTxtEmail().getText());
		customer.setWeb(customerView.getTxtWeb().getText());
		customer.setFax(customerView.getTxtFax().getText());
		
		customer.setGeburtstag(customerView.getBirthday());
		
	}

	private void updateGuiFieldsFromRoom() {
		customerView.getTxtFirstName().setText(customer.getVorname());
		customerView.getTxtSecondName().setText(customer.getNachname());
		customerView.getTxtAdressOfBill().setText(
				customer.getRechnungsadresse());
		customerView.getTxtCompanyname().setText(customer.getFirmenname());
		customerView.getTxtNotice().setText(customer.getNotiz());
		customerView.getTxtDiscount().setText("" + customer.getRabat());
		customerView.getTxtTelephoneNumber().setText(customer.getTelefon());
		customerView.getTxtEmail().setText(customer.getEmail());
		customerView.getTxtWeb().setText(customer.getWeb());
		customerView.getTxtFax().setText(customer.getFax());

		customerView.setBirthday(customer.getGeburtstag());
		
	}

}
